package com.cloudeasy.provider.service.impl;

import com.cloudeasy.base.constant.GlobalConstant;
import com.cloudeasy.core.model.dto.GlobalExceptionLogDto;
import com.cloudeasy.core.support.BaseService;
import com.cloudeasy.provider.mapper.MdcExceptionLogMapper;
import com.cloudeasy.provider.model.domain.MdcExceptionLog;
import com.cloudeasy.provider.model.dto.MdcExceptionQueryDto;
import com.cloudeasy.provider.model.dto.robot.ChatRobotMsgDto;
import com.cloudeasy.provider.model.factory.ChatRobotMsgFactory;
import com.cloudeasy.provider.service.MdcExceptionLogService;
import com.cloudeasy.provider.service.OpcRpcService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Create By 郭金宝 on 2019/1/17.<br>
 */
@Service
public class MdcExceptionLogServiceImpl extends BaseService<MdcExceptionLog> implements MdcExceptionLogService {
    @Resource
    private MdcExceptionLogMapper mdcExceptionLogMapper;
    @Resource
    private TaskExecutor taskExecutor;
    @Resource
    private OpcRpcService opcRpcService;
    @Value("${cloudeasy.dingTalk.webhookToken.sendException}")
    private String webhookToken;

    @Override
    public void saveAndSendExceptionLog(final GlobalExceptionLogDto exceptionLogDto) {
        MdcExceptionLog exceptionLog = new ModelMapper().map(exceptionLogDto, MdcExceptionLog.class);

        exceptionLog.setId(generateId());
        exceptionLog.setCreateTime(new Date());
        mdcExceptionLogMapper.insertSelective(exceptionLog);

        taskExecutor.execute(() -> {
            if (judgeIsSend(exceptionLogDto.getProfile())) {
                String text = exceptionLog.getApplicationName() + "出现异常. 环境：" + exceptionLogDto.getProfile() + "，操作人：" + exceptionLogDto.getCreator() + ".异常类型：" + exceptionLogDto.getExceptionSimpleName();
                ChatRobotMsgDto chatRobotMsgDto = ChatRobotMsgFactory.createChatRobotTextMsg(webhookToken, text, false, null);
                opcRpcService.sendChatRobotMsg(chatRobotMsgDto);
            }
        });

    }

    @Override
    public PageInfo queryExceptionListWithPage(final MdcExceptionQueryDto mdcExceptionQueryDto) {
        PageHelper.startPage(mdcExceptionQueryDto.getPageNum(), mdcExceptionQueryDto.getPageSize());
        List<MdcExceptionLog> actionList = mdcExceptionLogMapper.queryExceptionListWithPage(mdcExceptionQueryDto);
        return new PageInfo<>(actionList);
    }

    private boolean judgeIsSend(String profile) {
        Calendar calendar = Calendar.getInstance();
        int time = calendar.get(Calendar.HOUR_OF_DAY);
        return GlobalConstant.PRO_PROFILE.equals(profile) || time >= 10 && time <= 18;
    }
}